//
//  SLCAAnimationSpeedController.m
//  Objective-C
//
//  Created by S_Line on 2018/7/3.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCAAnimationSpeedController.h"

@interface SLCAAnimationSpeedController ()

@property (nonatomic, weak) CALayer *colorLayer;
@property (nonatomic, weak) UIView *colorView;

@end

@implementation SLCAAnimationSpeedController

- (void)initNavBar {
    self.navTitle = @"AnimationSpeed 动画速度";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    // create a red layer
    CALayer *colorLayer = [CALayer layer];
    colorLayer.frame = CGRectMake(0, 0, 100, 100);
    colorLayer.position = CGPointMake(kScreenWidth / 2.0, kScreenHeight / 2.0);
    colorLayer.backgroundColor = [UIColor blueColor].CGColor;
    self.colorLayer = colorLayer;
    [self.view.layer addSublayer:colorLayer];
    
    UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    self.colorView = colorView;
    colorView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:colorView];
    
    UIButton *changeButton = [[UIButton alloc] initWithFrame:CGRectMake(100, kNavigationBarHeight, 100, 40)];
    [changeButton setTitle:@"Change" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [changeButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(changeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // configure the transaction
    [CATransaction begin];
    [CATransaction setAnimationDuration:1.0];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    // set the position
    self.colorLayer.position = [[touches anyObject] locationInView:self.view];
    // commit transaction
    [CATransaction commit];
    
    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.colorView.center = [[touches anyObject] locationInView:self.view];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)changeButtonClick:(UIButton *)sender {
    // create a keyframe animation
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"backgroundColor";
    animation.duration = 2.0;
    animation.values = @[
                         (__bridge id)[UIColor blueColor].CGColor,
                         (__bridge id)[UIColor redColor].CGColor,
                         (__bridge id)[UIColor greenColor].CGColor,
                         (__bridge id)[UIColor blueColor].CGColor
                         ];
    // add timing function
    CAMediaTimingFunction *fn = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.timingFunctions = @[fn, fn, fn];
    // apply animation to layer
    [self.colorLayer addAnimation:animation forKey:nil];
}

@end
